<?php
/*
Plugin Name: BUETian.com group-manager [Group post] [BOT]
Plugin URI: http://www.buetian.com
Description: Posts from a group will be imported
Author: seoul
Version: 1.0
*/
function register_my_session_start_fb()
{
  if( !session_id() )
  {
    session_start();
  }
}
add_action('init', 'register_my_session_start_fb');


function ADD_WPFB_GROUP_IMPORTER() {
// default files which are necessary will go here;
}
add_action( 'admin_footer', 'ADD_WPFB_GROUP_IMPORTER' );

add_action('admin_menu', 'wpfb_plugin_menu');

function wpfb_plugin_menu() {
	add_menu_page("WPFB- Admin- Options", "FB Groups", 'manage_options', 'wpfb-top-level-handle', 'wpfb_sublevel_home' );
	// Add a submenu to the custom top-level menu:

	// Add a submenu to the custom top-level menu:
    add_submenu_page('wpfb-top-level-handle', "Settings", "Settings", 'manage_categories', 'wpfb-top-level-handle', 'wpfb_sublevel_settings');
	
	// Add a submenu to the custom top-level menu:
	add_submenu_page('wpfb-top-level-handle', "Insert new group", "Insert new group", 'manage_categories', 'wpfb-sub-insert-group', 'wpfb_sublvl_insert_grp');

	// Add a submenu to the custom top-level menu:
    add_submenu_page('wpfb-top-level-handle', "Import group posts to server", "Import manually", 'manage_categories', 'wpfb-sub-import-post-json', 'wpfb_sub_import_group_posts_as_json');

	// Add a submenu to the custom top-level menu:
    add_submenu_page('wpfb-top-level-handle', "View posts from database", "View database", 'manage_categories', 'wpfb-sub-view-post-database', 'wpfb_sub_view_post_database');	
	
	// Add a submenu to the custom top-level menu:
    add_submenu_page('wpfb-top-level-handle', "Initiate crawl", "Initiate crawl", 'manage_categories', 'wpfb-sub-view-initiate-crawl', 'wpfb_sub_init_crawl');
}

function wpfb_sublevel_home()
{
	;
}

add_action( 'wp_ajax_wpfb_crawler', 'wpfb_crawler_action_callback' );
function wpfb_crawler_action_callback() {
	include_once ('wpfb.group.crawler.php');
	try{
		$wpfb = new CrawlManager('wpfb-sub-view-initiate-crawl');
		$wpfb->pushToDatabase();
	}catch (Exception $e) {
		echo "<h3>Caught exception: <font style='color:red'>{$e->getMessage()}</font></h3><hr/>";
	}
}

function wpfb_sub_init_crawl()
{
	include_once ('wpfb.group.crawler.php');
	try{
		$wpfb = new CrawlManager('wpfb-sub-view-initiate-crawl');
		$wpfb->crawlerFrontEnd();
	}catch (Exception $e) {
		echo "<h3>Caught exception: <font style='color:red'>{$e->getMessage()}</font></h3><hr/>";
	}
}

function wpfb_sub_view_post_database()
{
	include_once ('wpfb.group.view_database.php');
	try{
		$wpfb = new ViewDatabase('wpfb-sub-view-post-database');
		$wpfb->top500Post();
	}catch (Exception $e) {
		echo "<h3>Caught exception: <font style='color:red'>{$e->getMessage()}</font></h3><hr/>";
	}
}

function wpfb_sublevel_settings()
{
	include_once('wpfb.group.home.php');
	try{
		$object= new Settings('wpfb-top-level-handle');
		$object->renderHTMLforGroup();
	}catch (Exception $e) {
		echo "<h3>Caught exception: <font style='color:red'>{$e->getMessage()}</font></h3><hr/>";
	}
}

function wpfb_sublvl_insert_grp()
{
	include_once ('wpfb.group.all.php');
	try{
		$wpfb = new GroupManager('wpfb-sub-insert-group');
		$wpfb->newGroupInsert();
	}catch (Exception $e) {
		echo "<h3>Caught exception: <font style='color:red'>{$e->getMessage()}</font></h3><hr/>";
	}
}

function wpfb_sub_import_group_posts_as_json()
{
	include_once ('wpfb.group.all.php');
	try{
		$wpfb = new GroupManager('wpfb-sub-import-post-json');
		$wpfb->importPostsIntoServer();
	}catch (Exception $e) {
		echo "<h3>Caught exception: <font style='color:red'>{$e->getMessage()}</font></h3><hr/>";
	}
}


?>